package com.zhengke.controller.hubf;

import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;
import com.zhengke.common.object.LoginObject;
import com.zhengke.pojo.AjaxResult;
import com.zhengke.pojo.CheckOutCon;
import com.zhengke.pojo.CheckOutUser;
import com.zhengke.pojo.SysOrderResult;
import com.zhengke.service.checkout.CheckoutCounterService;

/**
 * @author 黄保富
 * @date 2020年1月16日  
 * @version 1.0.0 
 * 收银台的控制层
 */
@Controller
@RequestMapping("/checkout")
public class CheckoutCounterController {
	
	@Autowired
	CheckoutCounterService checkService;
	
	public static final String SESSION_USER_KEY = "userinfo";

	public static final String USER_POPEDOM_KEY = "userpopedom";
	
	//首先是进入收银台的主界面的方法
	@RequestMapping("/toCheckPage.do")
	public String getoCheckMethod(HttpServletRequest request,HttpServletResponse response,ModelMap model) {
		double goodsCount = 0.00;//用户的订单的总金额
		HttpSession session = request.getSession();
		//首先将值传递过去进行验证
		LoginObject loginMessge = (LoginObject) session.getAttribute(SESSION_USER_KEY);
		String user_name = loginMessge.getLoginName();
		//通过登录到sys_user的信息将对应的tv_user的信息获取到
		CheckOutUser checkUser = checkService.querryTvUserBySysUserLoginName(user_name);
		String user_tvn = checkUser.getUser_id();//用户的id或者是用户的tvn号
		checkUser.setUserName(user_name);
		//根据用户的id(user_tvn)查询对应所有状态为30,40的订单
		Map<String,Object> messageMap = new HashMap<String, Object>();
		messageMap.put("user_id", user_tvn);
		List<CheckOutCon> checkOutCon =	checkService.querryOrderListWhereInSAF(messageMap);
		//遍历集合获取所有的订单中商品的数量个价格，并获取对应的总金额
		for(CheckOutCon check : checkOutCon) {
			String number = check.getGoods_num();//得到商品的数量
			String price = check.getGoods_price();//得到商品 的单价
			double onumber = Double.parseDouble(number);
			double oprice = Double.parseDouble(price);
			double pay = onumber*oprice;
			goodsCount += pay;
		}
		DecimalFormat df = new DecimalFormat("0.00");
		String payTotal = df.format(goodsCount);
		//将信息传递过去包括用户的id
		model.put("checkMessage",checkUser);
		model.put("orderPay",payTotal);
		return "checkout/chechou";
	}
	
	//点击添加商品的时候进行添加商品的时候的界面
	@RequestMapping("/addGoods.do")
	public String addGoodsMethod(String userId,ModelMap modle) {//获取对应的
		//获取成功之后查找订单
		//根据用户的id(user_tvn)查询对应所有状态为30,40的订单
		Map<String,Object> messageMap = new HashMap<String, Object>();
		messageMap.put("user_id", userId);
		List<CheckOutCon> checkOutCon =	checkService.querryOrderListWhereInSAF(messageMap);
		//获取之后进行遍历然后进行信息的展示，得单订单的id 订单对应的商品信息 订单对应商品的数量  订单对应商品的售价
		modle.put("list",checkOutCon);
		return "checkout/addGoods";
	}
	
	
	//点击减少商品的时候跳转的页面
	@RequestMapping("/calcGoods.do")
	public String calcGoodsMethod(String userId,ModelMap modle) {
		//获取成功之后查找订单
		//根据用户的id(user_tvn)查询对应所有状态为30,40的订单
		Map<String,Object> messageMap = new HashMap<String, Object>();
		messageMap.put("user_id", userId);
		List<CheckOutCon> checkOutCon =	checkService.querryOrderListWhereInSAF(messageMap);
		//获取之后进行遍历然后进行信息的展示，得单订单的id 订单对应的商品信息 订单对应商品的数量  订单对应商品的售价
		modle.put("list",checkOutCon);
		return "checkout/calcGoods";
	}
	
	//添加商品的方法
	@RequestMapping("/addGoodsReturn.do")
	@ResponseBody
	public AjaxResult addGoodsMethodReturn(HttpServletRequest request,HttpServletResponse response,String orderID,String goodsNumber) {
		try {
		//获取表单中的数据进行数据库的操作然后进行刷新表格的操作
		Map<String,Object> messageMap = new HashMap<String, Object>();
		//首先进行查询然后进行添加的操作
		String oldGoodsNumber = checkService.querryOrderToGoodsNumber(orderID);
		double firstgoodsNumber = Double.parseDouble(goodsNumber);
		double firstNumber = Double.parseDouble(oldGoodsNumber);
		double newNuberr = firstgoodsNumber+firstNumber;
		String caOrderIdString = checkService.querryOrderIdByOrderSn(orderID);
		messageMap.put("order_id", caOrderIdString);
		messageMap.put("goods_num", newNuberr);
		//进行修改
		int result = checkService.updateOrderGoodsNumByUserId(messageMap);
			if(result>0) {
				return new AjaxResult("200","！添加成功！");
			}else {
				return new AjaxResult("300","**添加失败**");
			}
		}catch (Exception e) {
			return new AjaxResult("300","··系统异常··");
		}
	}
	
	//减少商品的方法
	@RequestMapping("/calscGoodsReturn.do")
	@ResponseBody
	public AjaxResult calscOrderGoodsMethodReturn(HttpServletRequest request,HttpServletResponse response,String orderID,String goodsNumber) {
		try {
		//获取表单中的数据进行数据库的操作然后进行刷新表格的操作
		Map<String,Object> messageMap = new HashMap<String, Object>();
		//首先进行查询然后进行添加的操作
		String oldGoodsNumber = checkService.querryOrderToGoodsNumber(orderID);
		double firstgoodsNumber = Double.parseDouble(goodsNumber);
		double firstNumber = Double.parseDouble(oldGoodsNumber);
		double newNuberr = firstNumber - firstgoodsNumber;
		String caOrderIdString = checkService.querryOrderIdByOrderSn(orderID);
		messageMap.put("order_id", caOrderIdString);
		messageMap.put("goods_num", newNuberr);
		//进行修改
		int result = checkService.updateOrderGoodsNumByUserId(messageMap);
			if(result>0) {
				return new AjaxResult("200","！添加成功！");
			}else {
				return new AjaxResult("300","**添加失败**");
			}
		}catch (Exception e) {
			return new AjaxResult("300","··系统异常··");
		}
	}
	
	//表格数据查询
	@RequestMapping("/querryOrderList.do")
	@ResponseBody
	public SysOrderResult querryOrderlistByUserTvn(String tvnNo,HttpServletRequest request ,HttpServletResponse response,int page,int limit,String selectOrder,String startTime,String endTime){
		PageHelper.startPage(page, limit, true);//开启分页
		Map<String,Object> messageMap = new HashMap<String, Object>();
		messageMap.put("user_id", tvnNo);
		messageMap.put("selectOrder", selectOrder);
		messageMap.put("startTime", startTime);
		messageMap.put("endTime", endTime);
		//获取到查询的订单的集合
		List<CheckOutCon>  checkLlistCheckOutCons = checkService.querryOrderListWhereInSAF(messageMap);
		//设置返回的值类型
		SysOrderResult sysOrderResult = new SysOrderResult();
		sysOrderResult.setCode("0");
		sysOrderResult.setData(checkLlistCheckOutCons);
		sysOrderResult.setCount(7L);
		return sysOrderResult;
	}
	
	//结算订单的方法
	public String gotoDelaOrderMethod() {
		
		return "";
	}
	
}
